package com.offcn.service.Impl;

import com.offcn.bean.User;
import com.offcn.dao.Impl.UserDaoImpl;
import com.offcn.dao.UserDao;
import com.offcn.service.UserService;
import com.offcn.utils.GetNewTime;
import com.offcn.utils.Page;
import com.offcn.utils.ReturnResult;

/**
 * @Author: wy
 * @Date: 2022/9/5 19:20
 */
public class UserServiceImpl implements UserService {

    private UserDao userDao = new UserDaoImpl();

    //判断 用户是否登陆的
    @Override
    public User doLogin(String username, String password) {
        return userDao.doLogin(username, password);
    }

    @Override
    public Page<User> getUserListPage(String pageSize, String pageNo, String name) {
        //处理数据
        int pno = 1;
        try {
            pno = Integer.parseInt(pageNo);
        } catch (Exception e) {
        }
        int psize = 1;
        try {
            psize = Integer.parseInt(pageSize);
        } catch (Exception e) {
        }
        if (null == name) {
            name = "";
        }
        return userDao.getUserListPage(pno, psize, name);
    }

    @Override
    public User getUserByUsername(String username) {

        return userDao.getUserByUsername(username);
    }

    @Override
    public User doExistPhone(String phone) {

        return userDao.doExistPhone(phone);
    }

    @Override
    public int AddUser(String name, String phone, String age,
                       String sex, String username, String password,
                       String status, String role) {
        int agei = Integer.parseInt(age);
        int sexi = Integer.parseInt(sex);
        int statusi = Integer.parseInt(status);
        int rolei = Integer.parseInt(role);

        User user = new User(name, phone, agei, sexi, username, password, null, statusi, GetNewTime.getNewTime(), rolei);

        return userDao.AddUser(user);
    }

    @Override
    public int updateUser(String name, String phone, String age, String sex,
                          String password, String status, String role, String uid) {

        int agei = Integer.parseInt(age);
        int sexi = Integer.parseInt(sex);
        int statusi = Integer.parseInt(status);
        int rolei = Integer.parseInt(role);
        User user = new User(name, phone, agei, sexi, null, password, null, statusi, null, rolei);
        user.setUid(Integer.parseInt(uid));
        return userDao.updateUser(user);
    }

    @Override
    public ReturnResult deleteUser(String uids, User userSession) {
        String[] split = uids.split(",");
        System.out.println(split.length);
        ReturnResult returnResult = null;
        for (String uid : split) {
            if (uid.equals(userSession.getUid() + "")) {
                //意味着当前用户中有登录者 就不能删除
                returnResult = new ReturnResult("[" + userSession.getUsername() + "]不能删除" +
                        "他是当前登录用户", -1, null);
                return returnResult;
            }
        }

        try {
            userDao.deleteUser(uids);
            returnResult = new ReturnResult("删除成功", 1, null);

        } catch (Exception e) {
            returnResult = new ReturnResult("删除失败", -1, null);
            throw new RuntimeException(e);
        }
        return returnResult;
    }


}
